﻿namespace Smart.Service
{
    using Smart.Utils.Security2;
    using System;
    using System.IO;

    internal class FileReadWrite
    {
        public static string[] Data = new string[] { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
        private static byte[] iv = new byte[8];
        public static byte[] key = new byte[] { 
            0x15, 10, 0x40, 10, 100, 40, 200, 4, 0x15, 0x36, 0x41, 0xf6, 5, 0x3e, 1, 0x36, 
            0x36, 6, 8, 9, 0x41, 4, 0x41, 9
         };

        public static void ExportFile(string FilePath, string[] Data)
        {
            FileStream output = File.Create(FilePath);
            BinaryWriter writer = new BinaryWriter(output);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < Data.Length; j++)
                {
                    writer.Write(Data[j]);
                }
            }
            writer.Close();
            output.Close();
        }

        public static string ReadFile(string FilePath)
        {
            FileInfo info = new FileInfo(FilePath);
            if (!info.Exists)
            {
                return string.Empty;
            }
            FileStream input = File.OpenRead(FilePath);
            BinaryReader reader = new BinaryReader(input);
            string str = reader.ReadString();
            reader.Close();
            input.Close();
            return str;
        }

        public static string ReadFile(string FilePath, string Key)
        {
            FileInfo info = new FileInfo(FilePath);
            if (!info.Exists)
            {
                return string.Empty;
            }
            FileStream input = File.OpenRead(FilePath);
            BinaryReader reader = new BinaryReader(input);
            for (int i = 0; i < Data.Length; i++)
            {
                Data[i] = MyEncryption.Decrypt(reader.ReadString(), Key, true);
            }
            reader.Close();
            input.Close();
            return "OK";
        }

        public static void WriteFile(string FilePath, string Data)
        {
            FileStream output = File.Create(FilePath, 0x400, FileOptions.Encrypted);
            BinaryWriter writer = new BinaryWriter(output);
            writer.Write(Data);
            writer.Close();
            output.Close();
        }

        public static void WriteFile(string FilePath, string[] Data)
        {
            FileStream output = File.Create(FilePath);
            BinaryWriter writer = new BinaryWriter(output);
            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < Data.Length; j++)
                {
                    writer.Write(MyEncryption.Encrypt(Data[j], Data[0], true));
                }
            }
            writer.Close();
            output.Close();
        }

        public static void WriteFile(string FilePath, string[] Data, string Key)
        {
            FileStream output = File.Create(FilePath);
            BinaryWriter writer = new BinaryWriter(output);
            try
            {
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < Data.Length; j++)
                    {
                        writer.Write(MyEncryption.Encrypt(Data[j], Key, true));
                    }
                }
            }
            catch
            {
                throw new ArgumentException("File Data is bad");
            }
            writer.Close();
            output.Close();
        }
    }
}

